package com.cqie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.dto.entrepot.AddEntrepotWarehouseInfoDto;
import com.cqie.entity.EntrepotWarehouseInfo;
import com.cqie.exception.SpecialException;
import com.cqie.mapper.EntrepotWarehouseInfoMapper;
import com.cqie.service.IEntrepotWarehouseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-12-05
 */
@Service
public class EntrepotWarehouseInfoServiceImpl extends ServiceImpl<EntrepotWarehouseInfoMapper, EntrepotWarehouseInfo> implements IEntrepotWarehouseInfoService {

    @Resource
    private EntrepotWarehouseInfoMapper entrepotWarehouseInfoMapper;

    /**
     * 新增 仓库信息
     *
     * @param addEntrepotWarehouseInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addWarehouseInfo(AddEntrepotWarehouseInfoDto addEntrepotWarehouseInfoDto) throws SpecialException {

        if (ObjectUtils.isEmpty(addEntrepotWarehouseInfoDto.getWarehouseCode())) {
            throw new SpecialException("仓库编号不能为空");
        }

        if (ObjectUtils.isEmpty(addEntrepotWarehouseInfoDto.getWarehouseName())) {
            throw new SpecialException("仓库名称不能为空");
        }


        EntrepotWarehouseInfo warehouseInfo = new EntrepotWarehouseInfo();
        BeanUtils.copyProperties(addEntrepotWarehouseInfoDto, warehouseInfo);

        // 新增
        // 查看 仓库名称（编号） 不可重复
        var entrepotWarehouseInfo =
                entrepotWarehouseInfoMapper.getWarehouseInfoByNameOrCode(addEntrepotWarehouseInfoDto);

        if (!ObjectUtils.isEmpty(entrepotWarehouseInfo)) {
            throw new SpecialException("仓库名称（编号）不可重复");
        }

        return entrepotWarehouseInfoMapper.insert(warehouseInfo) > 0;


    }

    /**
     * 查询仓库List
     *
     * @param likeQuery 模糊查询
     * @param state     状态查询 0-全部 1未满 2已满
     * @param pageSize  每页条数
     * @param pageIndex 页码
     * @return
     */
    @Override
    public List<EntrepotWarehouseInfo> getWarehouseInfoList(String likeQuery, String state, Integer pageSize,
                                                            Integer pageIndex) {
        // 分页
        Page<EntrepotWarehouseInfo> page = new Page<>(pageIndex, pageSize);

        // 查询条件
        LambdaQueryWrapper<EntrepotWarehouseInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtils.isEmpty(likeQuery), EntrepotWarehouseInfo::getWarehouseName, likeQuery)
                .or()
                .like(ObjectUtils.isEmpty(likeQuery), EntrepotWarehouseInfo::getWarehouseCode, likeQuery);


        // 使用 MyBatis-Plus 的 selectPage 方法进行分页查询
        List<EntrepotWarehouseInfo> result =
                entrepotWarehouseInfoMapper.selectPage(page, lambdaQueryWrapper).getRecords();


        // 先查询每个仓库的当前数量

        if (Objects.equals(state, "1")) {


        } else if (Objects.equals(state, "2")) {

        }

        return result;
    }

    /**
     * 删除仓库信息
     *
     * @param warehouseId 仓库id
     * @return
     */
    @Override
    public Boolean delWarehouseInfo(Integer warehouseId) {
        return entrepotWarehouseInfoMapper.delWarehouseInfo(warehouseId);
    }

    /**
     * 计算仓库数量
     *
     * @return
     */
    @Override
    public Long getWarehouseInfoCount() {
        return entrepotWarehouseInfoMapper.getWarehouseInfoCount();
    }

    /**
     * 根据仓库id查询仓库信息
     * @param warehouseId 仓库id
     * @return 仓库信息
     */
    @Override
    public EntrepotWarehouseInfo getEntrepotWarehouseInfo(Integer warehouseId) {

        return entrepotWarehouseInfoMapper.getEntrepotWarehouseInfo(warehouseId);
    }


}
